package cn.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SysExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        SysException sysException = null;
        // 如果参数的异常类型为自定义的系统异常，则进行赋值
        if (e instanceof SysException) {
            sysException = (SysException) e;
        } else {
            sysException = new SysException("系统正在维护....");
        }
        // 创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        mv.addObject("errorMsg", sysException.getMessage());
        mv.setViewName("error");
        return mv;
    }
}